package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private SpuService spuService;

    @Resource
    private SpuDetailService spuDetailService;

    @Resource
    private SkuService skuService;

    /**
     * 分页查询spu
     * @param page     当前页
     * @param rows     每页大小
     * @param saleable 上架商品或下降商品
     * @param brandId 品牌id
     * @param categoryId 分类id
     * @param id  spu的id
     * @return 当前页商品数据
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "brandId",required = false) Long brandId,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "id",required = false) Long id,
            @RequestParam(value = "page",defaultValue = "1") Long page,
            @RequestParam(value = "rows",defaultValue = "5") Long rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ){
        PageDTO<SpuDTO> spuDTOPageDTO = spuService.querySpuByPage(brandId,categoryId,id,page,rows,saleable);
        return ResponseEntity.ok(spuDTOPageDTO);
    }

    /**
     * 新增商品
     * @param spuDTO 页面提交商品信息
     * @return 无
     */
    @PostMapping("/spu")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 更新商品
     * @param spuDTO 页面提交商品信息
     * @return 无
     */
    @PutMapping("/spu")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 修改商品上下架
     * @param id    商品spu的id
     * @param saleable  true：上架；false：下架
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(
            @RequestParam("id")Long id,
            @RequestParam("saleable") Boolean saleable
    ){
        spuService.updateSpuSaleable(id,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询spu及sku、spuDetail等
     * @param id 商品id
     * @return 商品信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){
        SpuDTO spuDTO = spuService.queryGoodsById(id);
        return ResponseEntity.ok(spuDTO);
    }

    /**
     * 根据id查询商品spu
     * @param id 商品id
     * @return 商品信息
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        SpuDTO spuDTO = spuService.querySpuById(id);
        return ResponseEntity.ok(spuDTO);
    }

    /**
     * 根据id查询detail
     * @param id 商品id
     * @return 商品的Detail
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> queryGoodsDetailById(@RequestParam("id") Long id){
        SpuDetailDTO spuDetailDTO = spuDetailService.queryGoodsDetailById(id);
        return  ResponseEntity.ok(spuDetailDTO);
    }

    /**
     * 根据spuID查询sku
     * @param id spuID
     * @return sku的集合
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuListBySpuId(@RequestParam("id") Long id){
        List<SkuDTO> skuDTOList = skuService.querySkuListBySpuId(id);
        return ResponseEntity.ok(skuDTOList);
    }

    /**
     * 根据id批量查询sku
     * @param ids spuID
     * @return sku的集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuListByIds(@RequestParam("ids") List<Long>ids){
        List<SkuDTO> skuDTOList = skuService.querySkuListByIds(ids);
        return ResponseEntity.ok(skuDTOList);
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     * @param id spu的id
     * @return 规格参数值
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching", required = false) Boolean searching){
        List<SpecParamDTO> specParamDTOList = spuDetailService.querySpecValues(id, searching);
        return ResponseEntity.ok(specParamDTOList);
    }

    /**
     * 扣减库存
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }

    /**
     * 加库存
     * @param cartMap 商品id及数量的map
     */
    @PutMapping("/stock/plus")
    public ResponseEntity<Void> addStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.addStock(cartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
